package quq.missq.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.ArrayList;
import quq.missq.Constants;
import quq.missq.R;
import quq.missq.activity.ImageViewPagerActivity;
import quq.missq.beans.Attach;
import quq.missq.utils.ImageLoadUtil;
import quq.missq.utils.VolleyTool;

/**
 * Created by River on 6/17/15.
 */
public class ImageGridViewAdapter extends BaseAdapter{

    private Context mContext;
    private ArrayList<Attach> attaches;

    public ImageGridViewAdapter(Context context,ArrayList<Attach> attaches){
        mContext = context;
        this.attaches = attaches;
    }

    public void setData(ArrayList<Attach> attaches){
        this.attaches = attaches;
    }

    @Override
    public int getCount() {
        return attaches.size();
    }

    @Override
    public Object getItem(int position) {
        return attaches.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = View.inflate(mContext, R.layout.image_gridview_adapter, null);
        ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
        ImageLoadUtil.loadImage(imageView,Constants.IMAGE_HOST + attaches.get(position).getThumbnail(),R.mipmap.empty_photo);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ImageViewPagerActivity.class);
                intent.putExtra("position",position);
                ArrayList<String> urls = new ArrayList<>();
                for (int i = 0;i < attaches.size();i++){
                    urls.add(Constants.IMAGE_HOST+attaches.get(i).getOrigin());
                }
                intent.putExtra("netImageUrls",urls);
                mContext.startActivity(intent);
            }
        });
        return view;
    }


}
